<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:converter="clr-namespace:Jerry.DroidBuilder.ThemeEditor.Converters"
                    xmlns:editor="clr-namespace:Jerry.DroidBuilder.ThemeEditor"
                    xmlns:local="clr-namespace:Jerry.DroidBuilder.ThemeEditor.Controls">

  <converter:DrawableValueConverter x:Key="Local_DrawableValueConverter" />

  <editor:DrawableContentTemplateSelector x:Key="Local_DrawableContentTemplateSelector" />

  <Style x:Key="Local_CleanButtonStyle" TargetType="{x:Type Button}">
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="{x:Type Button}">
          <ContentPresenter />
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>

  <ItemsPanelTemplate x:Key="Local_GenericContainerTemplate">
    <local:LinearLayout />
  </ItemsPanelTemplate>

  <ItemsPanelTemplate x:Key="Local_RelativeLayoutTemplate">
    <local:RelativeLayout />
  </ItemsPanelTemplate>

  <DataTemplate x:Key="{ComponentResourceKey {x:Type local:LayoutElement}, Template_GenericContainerTemplate}">
    <local:LayoutElementsControl DataContext="{Binding}"
                                 ItemsPanel="{StaticResource Local_GenericContainerTemplate}"
                                 ItemsSource="{Binding Children}" />
  </DataTemplate>

  <DataTemplate x:Key="{ComponentResourceKey {x:Type local:LayoutElement}, Template_RelativeLayoutTemplate}">
    <local:LayoutElementsControl DataContext="{Binding}"
                                 ItemsPanel="{StaticResource Local_RelativeLayoutTemplate}"
                                 ItemsSource="{Binding Children}" />
  </DataTemplate>

  <DataTemplate x:Key="{ComponentResourceKey {x:Type local:LayoutElement}, Template_ScrollContentTemplate}">
    <ScrollViewer>
      <local:LayoutElementsControl DataContext="{Binding}"
                                   ItemsPanel="{StaticResource Local_GenericContainerTemplate}"
                                   ItemsSource="{Binding Children}" />
    </ScrollViewer>
  </DataTemplate>

  <DataTemplate x:Key="{ComponentResourceKey {x:Type local:LayoutElement}, Template_ImageViewTemplate}">
    <ContentPresenter Content="{Binding src, Converter={StaticResource Local_DrawableValueConverter}}" ContentTemplateSelector="{StaticResource Local_DrawableContentTemplateSelector}" />
  </DataTemplate>

  <DataTemplate x:Key="{ComponentResourceKey {x:Type local:LayoutElement}, Template_TextViewTemplate}">
    <TextBlock x:Name="textView" Text="{Binding text}" FontSize="{Binding textSize, FallbackValue='15'}" Foreground="{Binding textColor, FallbackValue='#ffffffff'}">
      <!--<TextBlock.Effect>
        <DropShadowEffect 
      </TextBlock.Effect>-->
    </TextBlock>
    <DataTemplate.Triggers>
      <DataTrigger Binding="{Binding text, FallbackValue=''}" Value="">
        <Setter TargetName="textView" Property="Text" Value="{Binding Id}"/>
      </DataTrigger>
      <MultiDataTrigger>
        <MultiDataTrigger.Conditions>
          <Condition  Binding="{Binding Id, FallbackValue=''}" Value=""/>
          <Condition  Binding="{Binding text, FallbackValue=''}" Value=""/>
        </MultiDataTrigger.Conditions>
        <Setter TargetName="textView" Property="Text" Value="TEXT"/>
      </MultiDataTrigger>
    </DataTemplate.Triggers>
  </DataTemplate>

  <DataTemplate x:Key="{ComponentResourceKey {x:Type local:LayoutElement}, Template_ButtonTemplate}">
    <Button x:Name="buttonView" Content="{Binding text}" Foreground="White"/>
    <DataTemplate.Triggers>
      <DataTrigger Binding="{Binding text, FallbackValue=''}" Value="">
        <Setter TargetName="buttonView" Property="Content" Value="{Binding Id}"/>
      </DataTrigger>
      <MultiDataTrigger>
        <MultiDataTrigger.Conditions>
          <Condition  Binding="{Binding Id, FallbackValue=''}" Value=""/>
          <Condition  Binding="{Binding text, FallbackValue=''}" Value=""/>
        </MultiDataTrigger.Conditions>
        <Setter TargetName="buttonView" Property="Content" Value="TEXT"/>
      </MultiDataTrigger>
    </DataTemplate.Triggers>
  </DataTemplate>

  <!--  Following are the drawable templates.  -->

  <DataTemplate x:Key="{ComponentResourceKey {x:Type local:LayoutElement}, Template_ColorDrawableTemplate}">
    <Border Background="{Binding}" />
  </DataTemplate>

  <DataTemplate x:Key="{ComponentResourceKey {x:Type local:LayoutElement}, Template_ImageDrawableTemplate}">
    <Image Source="{Binding ImageSource}" />
  </DataTemplate>

  <DataTemplate x:Key="{ComponentResourceKey {x:Type local:LayoutElement}, Template_PatchedDrawableTemplate}">
    <local:NinePatchControl DisplayInfo="{Binding ., Converter={StaticResource Local_DrawableValueConverter}}" />
  </DataTemplate>

  <DataTemplate x:Key="{ComponentResourceKey {x:Type local:LayoutElement}, Template_CompositePatchedDrawableTemplate}">
    <Grid>
      <local:NinePatchControl x:Name="m_target" DisplayInfo="{Binding}" />
      <Button x:Name="m_button" Style="{StaticResource Local_CleanButtonStyle}" />
    </Grid>
    <DataTemplate.Triggers>
      <DataTrigger Binding="{Binding IsPressed, ElementName=m_button}" Value="True">
        <Setter TargetName="m_target" Property="DisplayInfo" Value="{Binding DrawableXml, Converter={StaticResource Local_DrawableValueConverter}, ConverterParameter=state_pressed}" />
      </DataTrigger>
    </DataTemplate.Triggers>
  </DataTemplate>

  <DataTemplate x:Key="{ComponentResourceKey {x:Type local:LayoutElement}, Template_CompositeDrawableTemplate}">
    <Grid>
      <Button x:Name="m_button" Content="{Binding}" ContentTemplate="{StaticResource ResourceKey={ComponentResourceKey {x:Type local:LayoutElement}, Template_ImageDrawableTemplate}}" Style="{StaticResource Local_CleanButtonStyle}" />
    </Grid>
    <DataTemplate.Triggers>
      <DataTrigger Binding="{Binding IsPressed, ElementName=m_button}" Value="True">
        <Setter TargetName="m_button" Property="Content" Value="{Binding DrawableXml, Converter={StaticResource Local_DrawableValueConverter}, ConverterParameter=state_pressed}" />
      </DataTrigger>
    </DataTemplate.Triggers>
  </DataTemplate>
</ResourceDictionary>